<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="调查对象" prop="respondent">
        <el-input
          v-model="queryParams.respondent"
          placeholder="请输入调查对象"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调查主题" prop="surveySubject">
        <el-input
          v-model="queryParams.surveySubject"
          placeholder="请输入调查主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调查人" prop="investigator">
        <el-input
          v-model="queryParams.investigator"
          placeholder="请输入调查人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调查时间" prop="surveyTime" >
         <el-date-picker clearable
            v-model="queryParams.surveyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择调查时间"
            :picker-options= "pickerOptions"
            @change="dateName()">
          </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:survey:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:survey:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:survey:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:survey:export']"
        >导出</el-button>
      </el-col>
        <audio controls="controls" hidden src="../voice/zhendong.mp3" ref="audio1"></audio>
        <audio controls="controls" hidden src="../voice/tips.mp3" ref="audio2"></audio>
        <audio controls="controls" hidden src="../voice/delete.mp3" ref="audio3"></audio>
        <audio controls="controls" hidden src="../voice/Download.mp3" ref="audio4"></audio>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="surveyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center"  />
      <el-table-column label="调查对象" align="center" prop="respondent" :show-overflow-tooltip="true"/>
      <el-table-column label="调查主题" align="center" prop="surveySubject" :show-overflow-tooltip="true"/>
      <el-table-column label="调查人" align="center" prop="investigator" />
      <el-table-column label="调查时间" align="center" prop="surveyTime" width="180" sortable>
       <template slot-scope="scope">
          <span>{{ parseTime(scope.row.surveyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

       <el-table-column label="调查状态" align="center" prop="surveyStatus">
        <template slot-scope="scope">
           <el-button
            size="mini"
            type="text"
          >{{ scope.row.surveyStatus == 1 ? "已调查":"未调查" }}</el-button>
        </template>
      </el-table-column>


      <el-table-column label="调查结果" align="center" prop="surveyResult" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:survey:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:survey:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLookOver(scope.row)"
            v-hasPermi="['system:survey:query']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务调查对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="调查对象" prop="respondent">
          <el-input v-model="form.respondent" placeholder="请输入调查对象" />
        </el-form-item>
        <el-form-item label="调查主题" prop="surveySubject">
          <el-input v-model="form.surveySubject" placeholder="请输入调查主题" />
        </el-form-item>
        <el-form-item label="调查人" prop="investigator">
          <el-input v-model="form.investigator" placeholder="请输入调查人" />
        </el-form-item>
        <el-form-item label="调查时间" prop="surveyTime" v-if="conceal">
          <el-date-picker clearable
            v-model="form.surveyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择调查时间"
            :picker-options= "pickerOptions"
            @change="dateName()">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="调查结果" prop="surveyResult" v-if="conceal">
          <el-input v-model="form.surveyResult" placeholder="请输入调查结果" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
      <!-- 查看调查弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="lookOver"
      width="500px"
      class="maskpopup"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="120px" size="mini">
        <el-row>
          <el-col :span="22">
            <el-form-item label="调查对象：">{{
              form.respondent
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="调查主题：">{{
              form.surveySubject
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="调查结果：">{{
              form.surveyResult
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="调查人：">{{
              form.investigator
            }}</el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="调查时间：">{{
              form.surveyTime
            }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { listSurvey, getSurvey, delSurvey, addSurvey, updateSurvey } from "@/api/system/survey";

export default {
  name: "Survey",
  data() {
    return {

      dataPicker: new Date() - 24 * 60 * 60 * 1000, //默认显示昨天时间
      pickerOptions: { //控制时间范围
          disabledDate (time) {
      return time.getTime() > (Date.now() )
}
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      //隐藏
      conceal:true,

      // 服务调查表格数据
      surveyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      lookOver:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        respondent: null,
        surveySubject: null,
        investigator: null,
        surveyTime: null,
        surveyStatus: null,
        surveyResult: null,
        fakeDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [{ required: true, message: '0表示正常,1表示异常', trigger: 'blur'},
      {pattern: /^[01]$/,message: '请输入0或1',trigger: 'blur'}],

        respondent: [
          { required: true, message: '请填写公司名称', trigger: 'blur'},
          { pattern: /\S/,message: '不能为空',trigger: 'blur'},
          { pattern: /^(?!.*[0-9])/,message: '不能包含数字',trigger: 'blur'},
          { pattern:/^\S+$/ ,message: '不能有空格',trigger: 'blur'}],

        surveySubject:[
          { required: true, message: '请填写主题', trigger: 'blur'},
          {pattern: /\S/,message: '不能为空',trigger: 'blur'}],

        investigator: [
          { required: true, message: '请填写调查人', trigger: 'blur'},
          { pattern: /\S/,message: '不能为空',trigger: 'blur'}],


      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询服务调查列表 */
    getList() {
      this.loading = true;
      listSurvey(this.queryParams).then(response => {
        this.surveyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.lookOver = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        surveyId: null,
        respondent: null,
        surveySubject: null,
        investigator: null,
        surveyTime: null,
        surveyStatus: null,
        surveyResult: null,
        fakeDelete: null
      };
      this.resetForm("form");
    },
      /** 查看详情 */
    handleLookOver(row) {
      getSurvey(row.surveyId).then((response) => {
        this.form = response.data;
        this.lookOver = true;
        this.title = "查看调查详情";
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.surveyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.audio2.currentTime=0;
      this.$refs.audio2.play();
      this.reset();
      this.open = true;
      this.title = "添加服务调查";
      this.conceal = false
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.audio2.currentTime=0;
      this.$refs.audio2.play();
      this.reset();
      const surveyId = row.surveyId || this.ids
      getSurvey(surveyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务调查";
        this.conceal = true
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.surveyId != null) {
            updateSurvey(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSurvey(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$refs.audio3.currentTime=0;
      this.$refs.audio3.play();
      const surveyIds = row.surveyId || this.ids;
      this.$modal.confirm('是否确认删除服务调查编号为"' + surveyIds + '"的数据项？').then(function() {
        return delSurvey(surveyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$refs.audio4.currentTime=0;
      this.$refs.audio4.play();
      this.download('system/survey/export', {
        ...this.queryParams
      }, `survey_${new Date().getTime()}.xlsx`)
    },
    dateName(){
      this.getList();
    }
  }
}
</script>
